<?php

// 店铺
namespace app\shop\controller;

use think\Db;
use think\Cache;
use think\Request;
use think\Session;
use think\Exception;
use think\Controller;
use app\shop\model\Shop as ShopModel;
use app\shop\model\Coupon as CouponModel;
use app\shop\model\ShopHome as ShopHomeModel;
use app\shop\model\ShopGoods as ShopGoodsModel;
use app\shop\model\MemberCollect as MemberCollectModel;
use app\shop\model\ShopGoodsType as ShopGoodsTypeModel;

class Shop extends Controller
{
    /**
     * 店铺主页
     * @param Request $request
     * @param ShopHomeModel $shopHome
     * @param CouponModel $coupon
     * @param ShopGoodsModel $shopGoods
     * @param ShopGoodsTypeModel $shopGoodsType
     * @return mixed
     */
    public function index(Request $request, ShopHomeModel $shopHome, CouponModel $coupon, ShopGoodsModel $shopGoods, ShopGoodsTypeModel $shopGoodsType)
    {
        // 获取店铺ID
        $shopId = $request->param('shop_id');

        // 店铺流量访问
        self::flow($shopId);

        // 获取店铺头部信息
        $shopInfo = self::publicShopHead($shopId);

        // 店铺首页信息
        $homeShop = $shopHome->where(['member_id' => $shopId])->field('member_id', true)->find()->toArray();

        // 店铺优惠券 及该用户优惠券使用情况
        $coupons = $coupon->with('CouponShop')->where(['member_id' => $shopId])->order(['sort' => 'desc'])->limit(4)->select()->toArray();

        $condition = ['member_id' => $shopId, 'state' => '1','status'=>1];

        // 店长推荐
        $shop_owner = $shopGoods->where($condition)->where('shop_recommend', 1)->field('id,title,price,thumb_img,sale_number')->order(['shop_sort' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'])->limit(7)->select()->toArray();

        // 首页超值单品
        $goodsList = $shopGoods->where($condition)->field('id,title,price,thumb_img,sale_number,market_price')->order(['shop_sort' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'])->limit(8)->select()->toArray();

        // 店铺商品分类
        $shopType = $shopGoodsType->where(['member_id' => $shopId, 'parenTid' => 0])->select();

        $shopCollect = self::collect_sence(2, $shopId, Session::get('member_id'));


        return $this->fetch('', [
            'good_list'  => $goodsList,
            'coupons'    => $coupons,
            'shop_info'  => $shopInfo,
            'home_shop'  => $homeShop,
            'shop_id'    => $shopId,
            'shop_types' => $shopType,
            'shop_owner' => $shop_owner,
            'shop_collect' => $shopCollect,
        ]);
    }



    public function collect_sence($type, $collect_id, $uid)

    {

        $find = (new MemberCollectModel())->where(['type_id' => $type, 'collect_id' => $collect_id, 'member_id' => $uid])->find();



        return isset($find) ? 1 : 0;

    }


    /**
     * 店铺优惠券
     * @param Request $request
     * @param CouponModel $coupon
     * @return mixed
     */
    public function coupon(Request $request, CouponModel $coupon)
    {
        // 获取参数
        $id = $request->param('id');

        // 获取优惠卷
        $couponList = $coupon->where(['member_id' => $id, 'status' => '1', 'end_time' => ['gt', time()]])
            ->field('id,title,actual_price,virtual_price,number,content,start_time,end_time,sort as receive_status')
            ->with(['MemberCoupon' => function ($e) {
                $e->where(['member_id' => session('member_id')])->field('id,coupon_id,status,member_id');

            }])->order(['sort' => 'desc'])->paginate(10, false);


        return $this->fetch('', [
            'mate_head'   => '领卷中心',
            'coupon_list' => $couponList,
        ]);
    }


    /**
     * 全部商品
     */
    public function all_goods()
    {
        // 获取店铺ID
        $shopId = Request::instance()->param('shop_id');

        // 抛出异常
        if (empty($shopId)) throw new Exception('not Empty');
        // 获取筛选参数
        $inquire = Request::instance()->param();

        // 获取店铺头部信息
        $shopInfo = self::publicShopHead($shopId);

        // // 排除参加活动的商品
        // $limitStr = limit_str();

        // 店铺商品分类
        $category = ShopGoodsTypeModel::where(['member_id' => $shopId])->field('id,parenTid,title,parentStr')->select();

        // 搜索条件
        $condition['state'] = ['eq', 1];
        $condition['member_id'] = $shopId;
        // 是否开启审核
        if (Cache::get('web_commodity_audit') == 'Y') $condition['status'] = ['eq', '1'];

        // 条件
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['parenTid'])) $condition['parentStr'] = ['like', '%,' . $inquire['parenTid'] . ',%'];
        if (!empty($inquire['promotion'])) $condition['promotion'] = 1;
        // 修改此处!
        // if (!empty($inquire['high'])) $condition['price'] = ['between',[$inquire['low'],$inquire['high']]];


        // 排序
        $sort = ['shop_sort' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'];

        // 排序传参
        if (!empty($inquire['order'])) $sort = [$inquire['order'] => 'desc'];

        $result = ShopGoodsModel::where($condition)
            ->field('id,title,price,thumb_img,sale_number,rebate_percentage,evaluate_number')
            ->order($sort)
            ->paginate(9, false, ['query' => $inquire]);

        // 商品总数
        $total = ShopGoodsModel::where($condition)->count();

        // // 导航栏
        // if (!empty($inquire['parenTid'])) {
        //     $navigation = self::navigation($inquire['parenTid']);
        // } else {
        //     $navigation = '';
        // }

        return $this->fetch('', [
            'mate_head'  => '店铺分类',
            'data'       => $result,
            'shop_info'  => $shopInfo,
            'total'      => $total,
            // 'navigation' => $navigation,
            'cates'      => get_cate($category)
        ]);
    }


    /**
     * 店铺公共头部
     * @param $shop_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function publicShopHead($shop_id)
    {
        // 获取店铺信息
        if (Request::instance()->has('goods_id', 'get')) {
            Session::set('goods_id', Request::instance()->get('goods_id'));
        }
        // 获取店铺信息
        $shopInfo = (new ShopModel())->where('member_id', $shop_id)->field('logo,banner,title,name,description,collect_number,phone,create_time,member_id,top_banner,url,banner_id')->find();

        //店铺被收藏数
        $shopInfo['collect'] = (new MemberCollectModel())->where(['type_id' => 2, 'collect_id' => $shop_id])->count('id');
        $shopInfo['goods_id'] = Session::get('goods_id');

        return $shopInfo;
    }


    public function flow($shop_id)
    {
        Db::name('shop')->where('member_id',$shop_id)->setInc('hits');
    }


}